/**
 * @auth:Ning
 * purpose:For sound queue mangement...
 * date:2009-5-30	
 *  
 * */
package com.components.sound
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;

	public class SoundQueue
	{
		//START
		
		//PROPERTY
		private var arraySound:Array = [new SoundItem("mp3/mp3_01.mp3"),
						new SoundItem("mp3/mp3_02.mp3"),
						new SoundItem("mp3/mp3_03.mp3"),
						new SoundItem("mp3/mp3_04.mp3"),
						new SoundItem("mp3/mp3_05.mp3")];
						
		private var loadIndex:uint;

		private var playIndex:uint;		
		
		private var target:Sprite;		
		
		//CONSTURCOTR...
		
		/**
		 * add Event.SOUND_COMPLETE listeners to every sound item
		 * load the first sound...
		 * 
		 * */
		public function SoundQueue(target:Sprite)
		{
			
			this.target = target;
			
			for(var i:uint;i < this.arraySound.length;i++)
			{
				
				(this.arraySound[i] as SoundItem).addEventListener(Event.SOUND_COMPLETE,sndCompleteHandler);
				
			}
			
			loadSound(loadIndex);
						
		}
		
		//METHODS...
		
		//on sound item played complete....
		private function sndCompleteHandler(evt:Event):void
		{
			
			this.playIndex++;
			
			this.playSound(this.playIndex);
			
		}
		
		/**load sound, on the value of  parameter 'index' above 'length' property of  variable "arraySound" that store sounditem
		 *play the first sound, else load next sound...
		 * 
		 * */
		 
		private function loadSound(index:uint):void
		{
	
			if(index >= arraySound.length)
			{
				
				setStatus("All sounds are loaded complete... Ready to play...");
				
				playSound(playIndex)
		
				return;
		
			}
	
			var soundItem:SoundItem = arraySound[index] as SoundItem;
	
			soundItem.load();
			
			soundItem.addEventListener(ProgressEvent.PROGRESS,soundItemProgressHandler);
	
			soundItem.addEventListener(Event.COMPLETE,soundItemCompleteHandler);
		}
		
		
		//on sound item load complete...	
		private function soundItemCompleteHandler(evt:Event):void
		{
		
			loadIndex++;
	
			loadSound(loadIndex);
	
		}
		
		//on sound item is loading...
		private function soundItemProgressHandler(evt:ProgressEvent):void
		{
						
			setStatus("Sound" + (this.loadIndex + 1) + " is loading..." + Math.floor(evt.bytesLoaded / evt.bytesTotal * 100) + "%");
			
		}
		
		
		//play sound... parameter 'index' is index of variable 'arraySound' which you want to play...
		private function playSound(index:uint):void
		{
	
			index = index % arraySound.length;
		
			var soundItem:SoundItem = arraySound[index] as SoundItem;
	
			soundItem.play(1,new SoundEaseMangement(3000,["volume"],[[0,1]]),new SoundEaseMangement(3000,["volume"],[[1,0]]));
			
			setStatus("Sound" + (index + 1) + " is playing...");
			
		}
		
		//set the text property of  TextField 'txtStatus'... 
		private function setStatus(val:String):void
		{
			
			var txtStatus:TextField = this.target.getChildByName("txtStatus") as TextField;
			
			txtStatus.autoSize = TextFieldAutoSize.CENTER;
			
			txtStatus.text = val;
			
		}
		
		
		
		//END
	}
}